import 'package:flutter_admin/models/odoo/mps_order_line.dart';
import 'package:flutter_admin/pages/common/cry/cry_all.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/pages/common/cry/vo/select_option_vo.dart';
import 'package:flutter_admin/utils/utils.dart';
import 'package:intl/intl.dart';
import 'package:quiver/strings.dart';
import '../../../generated/l10n.dart';

class MpsOrderLineDialog extends StatefulWidget {
  String? comment;
  MpsOrderLineDialog({this.comment});

  @override
  _MpsOrderLineDialogState createState() => _MpsOrderLineDialogState();
}

class _MpsOrderLineDialogState extends State<MpsOrderLineDialog> {
  // late List<Widget> children;
  final GlobalKey<FormState> formKey = GlobalKey<FormState>();
  DateFormat format = DateFormat('yyyy-MM-dd');

  MpsOrderLine mpsOrderLine = MpsOrderLine();
  List<SelectOptionVO> availableList = [];
  List<SelectOptionVO> stateList = [];

  @override
  void initState() {
    super.initState();
    mpsOrderLine.dateDemandStart = Utils.getDate();
    mpsOrderLine.dateDemandEnd = Utils.getDate();
    _loadData();
  }

  _loadData() async {
    availableList = [
      SelectOptionVO(value: '', label: ""),
      SelectOptionVO(value: 'true', label: "是"),
      SelectOptionVO(value: 'false', label: "否"),
    ];
    if (widget.comment == 'draft') {
      stateList = [
        SelectOptionVO(value: '', label: ""),
        SelectOptionVO(value: 'draft', label: "新需求"),
        SelectOptionVO(value: 'purchase', label: "采购中"),
      ];
    }
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          CrySelectDate(
            context,
            width: 280,
            label: '交货开始时间',
            value: mpsOrderLine.dateDemandStart,
            lastDate: isNotBlank(mpsOrderLine.dateDemandEnd)
                ? format.parse(mpsOrderLine.dateDemandEnd!)
                : null,
            onDateChange: (v) {
              formKey.currentState?.save();
              mpsOrderLine.dateDemandStart = v;
              setState(() {});
            },
            onSaved: (v) {
              mpsOrderLine.dateDemandStart = v;
            },
          ),
          CrySelectDate(
            context,
            width: 280,
            label: '交货结束时间',
            value: mpsOrderLine.dateDemandEnd,
            firstDate: isNotBlank(mpsOrderLine.dateDemandStart)
                ? format.parse(mpsOrderLine.dateDemandStart!)
                : null,
            onDateChange: (v) {
              formKey.currentState?.save();
              mpsOrderLine.dateDemandEnd = v;
              setState(() {});
            },
            onSaved: (v) {
              mpsOrderLine.dateDemandEnd = v;
            },
          ),
          CryInput(
            width: 280,
            label: '订单号',
            value: mpsOrderLine.origin,
            onSaved: (v) {
              mpsOrderLine.origin = v;
            },
          ),
          CryInput(
            width: 280,
            label: '产品名称',
            value: mpsOrderLine.productName,
            onSaved: (v) {
              mpsOrderLine.productName = v;
            },
          ),
          CryInput(
            width: 280,
            label: '产品编码',
            value: mpsOrderLine.defaultCode,
            onSaved: (v) {
              mpsOrderLine.defaultCode = v;
            },
          ),
          CrySelect(
            width: 280,
            label: '是否齐套',
            dataList: availableList,
            value: mpsOrderLine.available,
            onSaved: (v) {
              mpsOrderLine.available = v;
            },
          ),
          widget.comment == 'draft'
              ? CrySelect(
                  width: 280,
                  label: '状态',
                  dataList: stateList,
                  value: mpsOrderLine.state,
                  onSaved: (v) {
                    mpsOrderLine.state = v;
                  },
                )
              : Text(''),
        ],
      ),
    );
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: "确定",
          onPressed: () {
            FormState form = formKey.currentState!;
            form.save();
            Navigator.pop(context, mpsOrderLine);
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text("查询"),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [form],
        ),
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }
}
